// ------------------------------------------------------------------------------
// name: searchEngine 搜索引擎
// author: cookie( h5devs.com/h5devs.net )
// created: 2018/12/1 22:30
// type:case|police|keyPerson|offenMan|leaveMan
// ------------------------------------------------------------------------------
// state
const State = {
  keyword: '',
  case: [], //案件
  police: [], //接处警
  keyPerson: [], //重点人员
  offenMan: [], //常口
  leaveMan: [] //暂口
};

// getters
const Getters = {
  getKeyWord: state => state.keyword,
  getCase: state => state.case,
  getPolice: state => state.police,
  getKeyPerson: state => state.keyPerson,
  getOffenMan: state => state.offenMan,
  getLeaveMan: state => state.leaveMan,
};

// mutations
const Mutations = {
  keyWordEdit(state, val) {
    state.keyword = val;
  },
  pushItem(state, data) {
    let { item, type } = data;
    let _data = state[type];
    // 判断是否有重复字段key,如果有，更新数据，没有就添加
    let bool = false;
    let index = 0;
    for (let i = 0; i < _data.length; i++) {
      let _item = _data[i];
      if (_item.key === item.key) {
        bool = true;
        index = i;
        break;
      }
    }
    if (bool) {
      _data.splice(index, 1, item);
      return;
    }
    _data.push(item);
    console.warn(state[type]);
  },
  deleteItem(state, data) {
    let {index,type} = data;
    state[type].splice(index, 1);
  }
};

// actions
const Actions = {
  // keyword edit
  setKeyWord({
    commit
  }, wordValue) {
    commit('keyWordEdit', wordValue);
  },
  //arry item push
  setItem({
    commit
  }, data) {
    console.warn(`${data.item}--${data.type}`);
    commit('pushItem', data);
  },
  // arry item del
  removeItem({
    commit
  }, data) {
    commit('deleteItem', data);
  }
};

export default {
  namespaced: true,
  strict: true,
  state: State,
  getters: Getters,
  actions: Actions,
  mutations: Mutations
};
